package com.xinbochuang.machine.admin.service.impl.system;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.system.SysStorehouse;
import com.xinbochuang.machine.admin.domain.system.SysStorehouseIn;
import com.xinbochuang.machine.admin.domain.system.SysStorehouseSecond;
import com.xinbochuang.machine.admin.mapper.system.SysStorehouseInMapper;
import com.xinbochuang.machine.admin.mapper.system.SysStorehouseMapper;
import com.xinbochuang.machine.admin.mapper.system.SysStorehouseSecondMapper;
import com.xinbochuang.machine.admin.service.system.ISysStorehouseSecondService;
import com.xinbochuang.machine.admin.service.system.ISysStorehouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黄晓鹏
 * @date 2020-09-30
 */
@Service
public class SysStorehouseServiceImpl extends ServiceImpl<SysStorehouseMapper, SysStorehouse> implements ISysStorehouseService {
    @Resource
    SysStorehouseMapper sysStorehouseMapper;

    @Resource
    SysStorehouseSecondMapper sysStorehouseSecondMapper;

    @Resource
    SysStorehouseInMapper sysStorehouseInMapper;

    @Resource
    private ISysStorehouseSecondService secondService;

    @Override
    public SysStorehouse getInPutDetailByQrCode(String qrcode) {
        QueryWrapper<SysStorehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysStorehouse::getCode, qrcode);
        SysStorehouse sysStorehouse = sysStorehouseMapper.selectOne(queryWrapper);
        if (sysStorehouse != null) {
            QueryWrapper<SysStorehouseSecond> secondQueryWrapper = new QueryWrapper<>();
            secondQueryWrapper.lambda().eq(SysStorehouseSecond::getFirstId, sysStorehouse.getId());
            List<SysStorehouseSecond> sysStorehouseSeconds = sysStorehouseSecondMapper.selectList(secondQueryWrapper);
            sysStorehouse.setSysStorehouseSecondList(sysStorehouseSeconds);
        }
        return sysStorehouse;
    }

    @Override
    public void input(String id,String storageRackId,String storageRackPosition) {
        SysStorehouse sysStorehouse = sysStorehouseMapper.selectById(Integer.valueOf(id));
        sysStorehouse.setStatus("3");
        sysStorehouse.setInTime(DateUtil.now());
        sysStorehouse.setStorageRackId(storageRackId);
        sysStorehouse.setStorageRackPosition(storageRackPosition);
        sysStorehouseMapper.updateById(sysStorehouse);
        QueryWrapper<SysStorehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysStorehouse::getStatus, "2").eq(SysStorehouse::getInId,sysStorehouse.getInId());
        Integer count = sysStorehouseMapper.selectCount(queryWrapper);
        if (count == 0) {
            SysStorehouseIn sysStorehouseIn = sysStorehouseInMapper.selectById(sysStorehouse.getInId());
            sysStorehouseIn.setStatus("3");
            sysStorehouseInMapper.updateById(sysStorehouseIn);
        }
    }
}
